﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace WPDataBinding
{
    
    public partial class ProductListPage : PhoneApplicationPage
    {
        List<Product> productList;
        public ProductListPage()
        {
            InitializeComponent();

            //Static Data
            //buildStaticProductList();

            //Call WebServices
            buildProductList();
        }

        public void buildStaticProductList() {
            productList = new List<Product>();
            productList.Add(new Product()
            {
                ID = 1,
                ProductName = "Nokia Lumia 930",
                ProductDescription = "Windows Phone 8.1 with Charging Cover",
                ProductImage = "http://product77.blob.core.windows.net/images/p1.jpg",
                CategoryID = 1
            });
            productList.Add(new Product()
            {
                ID = 2,
                ProductName = "iPhone 6S",
                ProductDescription = "Apple Phone with 5.5 inch screen ",
                ProductImage = "http://product77.blob.core.windows.net/images/p2.jpg",
                CategoryID = 2
            });
            productList.Add(new Product()
            {
                ID = 2,
                ProductName = "iPhone 6S",
                ProductDescription = "Apple Phone with 5.5 inch screen ",
                ProductImage = "http://product77.blob.core.windows.net/images/p2.jpg",
                CategoryID = 2
            });
            productList.Add(new Product()
            {
                ID = 2,
                ProductName = "iPhone 6S",
                ProductDescription = "Apple Phone with 5.5 inch screen ",
                ProductImage = "http://product77.blob.core.windows.net/images/p2.jpg",
                CategoryID = 2
            });

            productListBox.ItemsSource = productList;
        }
        public void buildProductList(){
            ProductService.ProductsSoapClient client = new ProductService.ProductsSoapClient();
            client.getProductListAsync();
            client.getProductListCompleted += client_getProductListCompleted;
        }

        void client_getProductListCompleted(object sender, ProductService.getProductListCompletedEventArgs e)
        {
            List<ProductService.Product> pL = e.Result.ToList<ProductService.Product>();
            productListBox.ItemsSource = pL;
        }
        public List<Product> getProductListFromAzure(){
             
            return null;
        }

        private void productListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ProductService.Product p = (ProductService.Product)   productListBox.SelectedItem;
            App a = App.Current as App;
            a.productTemp = p;
            a.poolData = p;
            NavigationService.Navigate(new Uri("/ProductDetail.xaml", UriKind.RelativeOrAbsolute));
        }
    }

    public class Product
    {
        public int ID { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }

        public string ProductImage { get; set; }
        public int CategoryID { get; set; }
    }
}